/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataanalysis.gui;

import dataanalysis.MyUtils;
import java.awt.Component;
import java.io.File;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import weka.core.Capabilities;
import weka.core.converters.AbstractSaver;
import weka.core.converters.ConverterUtils;
import weka.core.converters.FileSourcedConverter;
import weka.gui.ExtensionFileFilter;

/**
 *
 * @author Administrator
 */
public class MySaveDirChooser extends JFileChooser {

    private static Vector<ExtensionFileFilter> m_SaverFileFilters;

    static {
        initFilters(ConverterUtils.getFileSavers());
    }
    private Capabilities m_CapabilitiesFilter = null;
    private String m_folderName = "未命名";

    public MySaveDirChooser(File currentDirectory) {
        super(currentDirectory);
        this.setFileSelectionMode(DIRECTORIES_ONLY);
    }

    public void setFolderName(String str) {
        m_folderName = str;
    }

    @Override
    public int showSaveDialog(Component parent) {
        setFilters();
        int result = super.showSaveDialog(parent);

        if (result == CANCEL_OPTION) {
            return result;
        }

        String filepath = this.getSelectedFile().getAbsolutePath();

        String date = MyUtils.getDateString();

        this.setCurrentDirectory(new File(filepath));
        filepath = filepath + "\\" + m_folderName + "_" + date + "\\";
        this.setSelectedFile(new File(filepath));


        // overwrite the file?
        if ((result == APPROVE_OPTION) && (getSelectedFile().exists())) {
            int retVal = JOptionPane.showConfirmDialog(
                    parent,
                    "文件夹 '"
                    + getSelectedFile()
                    + "'\n已经存在 , 是否覆盖?");
            if (retVal == JOptionPane.OK_OPTION) {
                if (false == delFolderContent(getSelectedFile().getAbsolutePath())) {
                    JOptionPane.showMessageDialog(parent, "无法覆盖文件夹，其它程序可能正在使用！");
                    result = CANCEL_OPTION;
                } else {
                    result = APPROVE_OPTION;
                }
            } else if (retVal == JOptionPane.NO_OPTION) {
                result = showSaveDialog(parent);
            } else {
                result = CANCEL_OPTION;
            }
        }
        return result;
    }

    protected void setFilters() {
        //Vector<ExtensionFileFilter> list = filterSaverFileFilters(filterNonCoreSaverFileFilters(m_SaverFileFilters));
        Vector<ExtensionFileFilter> list = filterSaverFileFilters(m_SaverFileFilters);
        //Vector<ExtensionFileFilter> list = m_SaverFileFilters;
        for (int i = 0; i < list.size(); i++) {
            addChoosableFileFilter(list.get(i));
        }
        setAcceptAllFileFilterUsed(false);
        setFileFilter(list.get(0));
    }

    protected static void initFilters(Vector<String> classnames) {
        int i;
        int n;
        String classname;
        Class cls;
        String[] ext;
        String desc;
        FileSourcedConverter converter;
        ExtensionFileFilter filter;

        m_SaverFileFilters = new Vector<ExtensionFileFilter>();

        for (i = 0; i < classnames.size(); i++) {
            classname = (String) classnames.get(i);

            // get data from converter
            try {
                cls = Class.forName(classname);
                converter = (FileSourcedConverter) cls.newInstance();
                ext = converter.getFileExtensions();
                desc = converter.getFileDescription();
            } catch (Exception e) {
                cls = null;
                converter = null;
                ext = new String[0];
                desc = "";
            }

            if (converter == null) {
                continue;
            }

            for (n = 0; n < ext.length; n++) {
                filter = new ExtensionFileFilter(ext[n], desc + " (*" + ext[n] + ")");
                m_SaverFileFilters.add(filter);
            }

        }
    }

    public void setCapabilitiesFilter(Capabilities value) {
        m_CapabilitiesFilter = (Capabilities) value.clone();
    }

    protected Vector<ExtensionFileFilter> filterSaverFileFilters(Vector<ExtensionFileFilter> list) {
        Vector<ExtensionFileFilter> result;
        int i;
        ExtensionFileFilter filter;
        AbstractSaver saver;

        if (m_CapabilitiesFilter == null) {
            result = list;
        } else {
            result = new Vector<ExtensionFileFilter>();

            for (i = 0; i < list.size(); i++) {
                filter = list.get(i);
                saver = ConverterUtils.getSaverForExtension(filter.getExtensions()[0]);
                if (saver.getCapabilities().supports(m_CapabilitiesFilter)) {
                    result.add(filter);
                }
            }
        }

        return result;
    }

//删除文件夹
//param folderPath 文件夹完整绝对路径
    private boolean delFolder(String folderPath) {
        try {
            if (false == delFolderContent(folderPath)) {
                return false;
            } //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            if (false == myFilePath.delete()) {
                return false;
            } //删除空文件夹
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

//删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
    private boolean delFolderContent(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return false;
        }
        if (!file.isDirectory()) {
            return false;
        }
        String[] tempList = file.list();

        if (tempList.length == 0) {
            return true;
        }

        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                if (false == temp.delete()) {
                    return false;
                }
            }
            if (temp.isDirectory()) {
                if (false == delFolderContent(path + "/" + tempList[i])) {
                    return false;
                }//先删除文件夹里面的文件
                if (false == delFolder(path + "/" + tempList[i])) {
                    return false;
                }//再删除空文件夹
            }
        }
        return true;
    }
}
